//
//  CYUserCarFindWebVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/1/12.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYUserCarFindWebVC.h"
#import "MBProgressHUD+MJ.h"
#import "CYDiscoveryHttpTool.h"
#import "UIAlertView+Block.h"

@interface CYUserCarFindWebVC ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *carfindWebView;

@end

@implementation CYUserCarFindWebVC

- (void)viewDidLoad {
     
    [super viewDidLoad];
    [self loadData];
    self.title = @"查询";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)loadData {
    [MBProgressHUD showMessage:nil toView:self.view];
    [CYDiscoveryHttpTool creditQueryWithParam:self.param success:^(NSString *html) {
        [MBProgressHUD hideHUDForView:self.view];
        [self setupWebView:html];
    } failure:^(NSString *message, BOOL isLogin) {
        [MBProgressHUD hideHUDForView:self.view animated:NO];
        if (!isLogin) {
            
        } else {
            [MBProgressHUD showError:message];
        }
    }];
}

- (void)setupWebView:(NSString *)html {
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    webView.delegate = self;
    [webView loadHTMLString:html baseURL:nil];
    [self.view addSubview:webView];
    //添加滑动返回
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [webView addGestureRecognizer:rightSwipeGestureRecognizer];
}
/**
 *  添加滑动返回
 */
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController popViewControllerAnimated:YES];
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
    }
}

#pragma mark -
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
//     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//          if (buttonIndex == 0) {
//               [[self navigationController] setNavigationBarHidden:NO animated:NO];
//               [self.navigationController popViewControllerAnimated:YES];
//               
//          }
//     }];
     
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //如果加载完成webview后，navi隐藏,加载失败，提供系统导航栏给用户返回
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [MBProgressHUD hideHUDForView:self.view];
    webView.backgroundColor = CYNaviThemeColorAndroid;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    [MBProgressHUD hideHUDForView:self.view];
    NSString *url = request.URL.absoluteString;
     
     if([url rangeOfString:@"report"].location !=NSNotFound)//_roaldSearchText
     {
          NSString *str = url;
          CYLog(@"str = %@",str);
          NSString *str1 = [str substringFromIndex:12];//截取掉下标11之后的字符串
          CYLog(@"str1 = %@",str1);
          if (str1 && !str1.isEmpty) {
               
               UIPasteboard *pab = [UIPasteboard generalPasteboard];
               [pab setString:str1];
               if (pab == nil) {
                    [MBProgressHUD showError:@"复制失败"];
                    
               }else
               {
                    [MBProgressHUD showSuccess:@"已复制"];
                    
               }
               
               
          }
     }
     else
     {
          NSLog(@"no");
     }
    //h5内控件字段方法:searchinfo
     
    if([url hasSuffix:@"searchinfo"]) {
        //do something you want
        //返回上一级时，取消nav的隐藏
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
        //js交互oc获取到h5内的返回按钮后，点击返回，popvc
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}

@end
